﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Micua.Bootstrap.Elements
{
    public class HtmlElement : Micua.Utility.Abstract.ClearObjectMember, IDisposable, IHtmlString
    {
        private HtmlHelper _helper;

        private IDictionary<string, object> _htmlAttributes;
        private string _tagName;
        public HtmlElement(HtmlHelper helper,string tag)
        {
            this._htmlAttributes = new Dictionary<string, object>();
            this._tagName = tag;
            this._helper = helper;
        }

        public void AddHtmlAttribute(string name, string value)
        {
            _htmlAttributes.Add(name, value);
        }
        public void AddHtmlAttribute(IDictionary<string, object> htmlAttributes)
        {
            if (htmlAttributes == null) return;
            foreach (var item in htmlAttributes)
            {
                _htmlAttributes.Add(item.Key, item.Value);
            }
        }

        internal virtual string StartTag
        {
            get
            {
                if (string.IsNullOrEmpty(this._tagName)) return string.Empty;
                TagBuilder builder = new TagBuilder(this._tagName);
                builder.MergeAttributes<string, object>(this._htmlAttributes);
                return builder.ToString(TagRenderMode.StartTag);
            }
        }
        internal string EndTag
        {
            get
            {
                if (string.IsNullOrEmpty(this._tagName)) return string.Empty;
                return string.Format("</{0}>", this._tagName);
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public virtual string ToHtmlString()
        {
            return StartTag;
        }
        [EditorBrowsable(EditorBrowsableState.Never)]
        public void Dispose()
        {
            _helper.ViewContext.Writer.Write(EndTag);
        }
    }
}
